# coding: utf-8
from django.shortcuts import render
from rest_framework import permissions, status, views, viewsets, parsers
from rest_framework.decorators import detail_route, list_route
from rest_framework.response import Response
from rest_framework import pagination
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination
from django.db.models import Q
import operator
from operator import __or__ as OR
from functools import reduce

from configs.models.TipoDocumento import TipoDocumento


class GetPaginate(PageNumberPagination):
	page_size = 100
	page_size_query_param = 'page_size'
	max_page_size = 1000

	def get_paginated_response(self, data):
		return Response({
			'options': {
				'count': self.page.paginator.count,
				'pages': self.page.paginator.num_pages,
				'page': self.plus(),
				'next': self.siguente(),
				'previous': self.anterior(),
				'range': self.range()
			},
			'results': data
			})

	def siguente(self):
		if not self.page.has_next():
			return None
		page_number = self.page.next_page_number()
		return page_number

	def anterior(self):
		if not self.page.has_previous():
			return None
		page_number = self.page.previous_page_number()
		return page_number

	def plus(self):
		if not self.page.has_next():
			return None
		page = self.page.next_page_number()-1
		return page

	def range(self):
		total = self.page.paginator.count
		start = self.page.start_index()
		end = self.page.end_index()
		rangep = '{0} - {1} / {2}'.format(start, end, total)
		return rangep

class TipoDocumentoSerializer(serializers.ModelSerializer):
	class Meta:
		model = TipoDocumento
		fields = ('id', 'nombre', 'estado', 'created_at', 'updated_at',)
		read_only_fields = ('created_at', 'updated_at',)


class TipoDocumentoViewViewSet(viewsets.ModelViewSet):
	lookup_field = 'id'
	queryset = TipoDocumento.objects.all()
	serializer_class = TipoDocumentoSerializer
	pagination_class=GetPaginate


	def list(self, request, *args, **kwargs):
		query = request.query_params.get('query', None)
		field = request.query_params.get('field', 'id')
		order = request.query_params.get('order', '')

		if query is not None:
			queryall=(Q(id__icontains=query),
				Q(nombre__icontains=query))
			data = self.get_queryset().filter(reduce(OR, queryall)).order_by(''+order+''+field+'')
			page = self.paginate_queryset(data)
			if page is not None:
				serializer = self.get_serializer(page, many=True)
				return self.get_paginated_response(serializer.data)
		else:
			data = self.get_queryset().order_by(''+order+''+field+'')
			page = self.paginate_queryset(data)
			if page is not None:
				serializer = self.get_serializer(page, many=True)
				return self.get_paginated_response(serializer.data)

	def destroy(self, request, *args, **kwargs):
		objs=request.query_params.getlist('objs','')
		self.perform_destroy(objs)
		return Response({'results': objs})

	def perform_destroy(self, objs):
		data = self.get_queryset().filter(pk__in=objs)
		print('instancias -->',data)
		for r in data:
			r.delete()
	